package com.cloudptt.wp.ui.group.add.adapt;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ui.group.add.CallBack;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.db.model.UserInfo;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class UserSelectAdapter extends BaseAdapter{   

	private static int DAC_TYPE = 0;
	private static int CAMERA_TYPE = 1;
	private static int USER_TYPE = 2;
	private static int[] Drawable_online = {R.drawable.dac_icon_background_online, R.drawable.camera_icon, R.drawable.user_icon_background_online};
	private static int[] Drawable_offline = {R.drawable.dac_icon_background_offline, R.drawable.camera_icon, R.drawable.user_icon_background_offline};

	private static String TAG = "AddrListAdapter";
	private static final boolean DEBUG = true;
	private Context ctx = null;
	private List<UserInfo> userList = null;
	private McIdInfo mcIdInfo = null;
	private CallBack callBack = null;
	private Map<String,String> resultMap = new HashMap<String,String>();

	public UserSelectAdapter(Context ctx,List<UserInfo> userList,McIdInfo mcIdInfo,CallBack callBack)
	{
		Log.d(TAG, "AddrListAdapter");
		this.ctx = ctx;
		this.userList = userList;
		this.mcIdInfo = mcIdInfo;
		this.callBack = callBack;
	}

	private void setDrawableEnable(ImageView userIcon, boolean enable) {
		// TODO Auto-generated method stub
		int alpha = (int)(enable ? 255*1 : 255*0.5);
		userIcon.setImageAlpha(alpha);
	}

	private void setOffLineIcon(ImageView userIcon){
		userIcon.setBackgroundColor(Color.parseColor("#555555"));
	}


	private void showUserInfo(UserInfo info){
		if(DEBUG){
			CommDlg.show(ctx, info.toString());
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return userList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return userList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int childPosition, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ChildViewHolder childViewHolder;
		if (convertView == null){
			LayoutInflater inflater = LayoutInflater.from(ctx);
			convertView = inflater.inflate(R.layout.item_user_select, null);

			childViewHolder = new ChildViewHolder();
			childViewHolder.userInfoLy =  (View)  convertView.findViewById(R.id.user_info);
			childViewHolder.name    =  (TextView)  convertView.findViewById(R.id.user_name);
//			childViewHolder.num    =  (TextView)  convertView.findViewById(R.id.user_number);
			
			childViewHolder.userIcon = (ImageView)convertView.findViewById(R.id.user_icon);
			childViewHolder.mySelf = (TextView)convertView.findViewById(R.id.is_myself);
//			childViewHolder.divider = convertView.findViewById(R.id.child_divider);

			convertView.setTag(childViewHolder);
		}else{
			childViewHolder = (ChildViewHolder) convertView.getTag();
		}

		final CheckBox checkBtn  =  (CheckBox) convertView.findViewById(R.id.check_button);
//		checkBtn.setChecked(false);
		
		if(userList == null
				|| userList.get(childPosition) == null){
			return convertView;
		}
		final UserInfo userInfo = userList.get(childPosition);

//		if(childPosition == userInfoList.size()-1){
//			childViewHolder.divider.setVisibility(View.GONE);
//		}else{
//			childViewHolder.divider.setVisibility(View.VISIBLE);
//		}

		if(userInfo == null){
			return convertView;
		}
		String name = userInfo.getName();
		String mcId = userInfo.getAddrId();
//		String ip = userInfo.getIp();
		childViewHolder.name.setText(name);
		
		
		if(resultMap.get(mcId) == null)
		{
			Log.d(TAG, "getView setChecked false");
			checkBtn.setChecked(false);
		}
		else
		{
			Log.d(TAG, "getView setChecked true");
			checkBtn.setChecked(true);
		}

		if(userInfo.isDac())
		{//DAC
			checkBtn.setVisibility( View.VISIBLE );
			if(userInfo.getOnlineState() == 1){
				childViewHolder.userIcon.setImageResource(Drawable_online[DAC_TYPE]);
				setDrawableEnable(childViewHolder.userIcon, true);
//				setDrawableEnable(childViewHolder.checkBtn, true);
			}else{
				childViewHolder.userIcon.setImageResource(Drawable_offline[DAC_TYPE]);
				setDrawableEnable(childViewHolder.userIcon, false);
//				setDrawableEnable(childViewHolder.checkBtn, false);
			}
		}
		else if(userInfo.isCamera())
		{//Camera
			childViewHolder.userIcon.setImageResource(Drawable_online[CAMERA_TYPE]);
			checkBtn.setVisibility( View.VISIBLE );

		}
		else
		{//normal user
			if(userInfo.getOnlineState() == 1)
			{
				childViewHolder.userIcon.setImageResource(Drawable_online[USER_TYPE]);
				setDrawableEnable(childViewHolder.userIcon, true);
//				setDrawableEnable(childViewHolder.checkBtn, true);
//				mcId = mcId + "(" + ip+")";
			}else{
				childViewHolder.userIcon.setImageResource(Drawable_offline[USER_TYPE]);
				setDrawableEnable(childViewHolder.userIcon, false);
//				setDrawableEnable(childViewHolder.checkBtn, false);
			}
			
//			childViewHolder.num.setText(mcId);
			checkBtn.setVisibility( View.VISIBLE );
		}

		if(userInfo.isSameOne(mcIdInfo)){
			//childViewHolder.mySelf.setVisibility(View.VISIBLE);
			String str = ctx.getResources().getString(R.string.name_myself);
			//childViewHolder.mySelf.setVisibility(View.GONE);
			childViewHolder.name.setText(name+"."+str);
		}else{
			childViewHolder.name.setText(name);
			//childViewHolder.mySelf.setVisibility(View.GONE);
		}
//		final boolean isSelected = childViewHolder.checkBtn.isChecked();
		convertView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) 
			{
				boolean isChecked = checkBtn.isChecked();
				Log.d(TAG, "doSelectAction isChecked = " + isChecked);
				isChecked = !isChecked;
				checkBtn.setChecked(isChecked);
				doSelectAction(isChecked,userInfo.getAddrId(),userInfo.getName());
			}
		});

		checkBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				boolean isChecked = checkBtn.isChecked();
				Log.d(TAG, "doSelectAction isChecked = " + isChecked);
				doSelectAction(isChecked,userInfo.getAddrId(),userInfo.getName());
			}
		});
		
//		childViewHolder.userIcon.setOnClickListener(new OnClickListener() {
//			public void onClick(View v) {
//				doSelectAction(isSelected,userInfo.getMcId());
//			}
//		});
		return convertView;
	}
	
	private void doSelectAction(boolean isSelected,String mcId,String name)
	{
		Log.d(TAG, "doSelectAction beging isSelected = " + isSelected + " mcId = " + mcId);
		Log.d(TAG, "doSelectAction beging resultMap = " + resultMap);
		if(isSelected)
		{
			resultMap.put(mcId,mcId);
		}
		else
		{
			resultMap.remove(mcId);
		}
		if(resultMap != null
				&& resultMap.size() > 0)
		{
			callBack.hasCheck(true);
		}
		else
		{
			callBack.hasCheck(false);
		}
		Log.d(TAG, "doSelectAction end resultMap = " + resultMap);
	}

	public Set<String> getResult() {
		return resultMap.keySet();
	}
	
	public String getDefaultName() {
		if(resultMap == null
				|| resultMap.size() <= 0)
		{
			return "default";
		}
		StringBuffer strBuf = new StringBuffer();
		for(Iterator<String> itor =  resultMap.keySet().iterator();itor.hasNext();)
		{
			String key = itor.next();
			String val = resultMap.get(key);
			strBuf.append(val);
			if(strBuf.length() > 10)
			{
				break;
			}
			strBuf.append(",");
		}
		String str = strBuf.toString();
		if(str.length() > 0)
		{
			str = str.substring(0, strBuf.length() - 1);
			if(str.length() > 10)
			{
				
			}
		}
		return "default";
	}

	public final class ChildViewHolder {
		public View userInfoLy;
		public TextView  name;
//		public TextView  num;
//		public CheckBox checkBtn;
		public ImageView userIcon;
		public TextView mySelf;
//		public View divider;
	}

	
}   


